<?php
namespace app\home\controller;

use think\Controller;
use Flc\Dysms\Client;
use Flc\Dysms\Request\SendSms;
use Cache;
use Db;
use think\facade\Session;
use app\home\model\Cart;


class User extends Controller
{   
    protected $middleware = [
        'Check'=> ['except' 	=> ['login','register','doregister','dologin'] ],
    ];

    public function login()
    {
        return $this->fetch('login');
    }
    public function register()
    {
        return $this->fetch('register');
    }
    //个人中心-我的订单
    public function home()
    {
        return $this->fetch('home-index');
    }
    //待付款页面
    public function home_order_pay()
    {
        return $this->fetch('home-order-pay');
    }
    //待发货页面
    public function home_order_send()
    {
        return $this->fetch('home-order-send');
    }
    //待发货页面
    public function home_order_receive()
    {
        return $this->fetch('home-order-receive');
    }
    //待评价页面
    public function home_order_evaluate()
    {
        return $this->fetch('home-order-evaluate');
    }
    //我的收藏页面
    public function home_person_collect()
    {
        return $this->fetch('home-person-collect');
    }
    //我的足迹页面
    public function home_person_footmark()
    {
        return $this->fetch('home-person-footmark');
    }
    //个人信息页面
    public function home_setting_info()
    {
        return $this->fetch('home-setting-info');
    }
    //地址管理  
    public function home_setting_address()
    {
        return $this->fetch('home-setting-address');
    }
    //安全管理
    public function home_setting_safe()
    {
        return $this->fetch('home-setting-safe');
    }
    //招商界面
    public function cooperation()
    {
        return $this->fetch('cooperation');
    }
    //招商规则
    public function sampling()
    {
        return $this->fetch('sampling');
    }
    //购物车界面
    public function cart()
    {   
        //获取购物车数据
        $data['cart_list'] = Cart::where(1)->select();
        $data['total_num'] = db('cart')->where(1)->sum('cart_goods_num');
        $res= Db::query('select sum(cart_goods_num*cart_goods_price) total_price from cart');
        $data['total_price'] = (int)$res[0]['total_price'];
        // echo '<pre>';
        // var_dump($data);die;
        return view('cart',$data);
    }
    //账号注册表单
    public function doregister()
    {   
        
        if(!isset($_POST['ml'])){
            $this->error('请勾选协议');
        }else{

            if($_SESSION['code']==$_POST['code']){
                // echo "<pre>";
                // var_dump($_POST);
                $num = Db::table('user')->where('user_name',$_POST['user_name'])->find();
                if($num==NULL){
                    $_POST['password'] = md5($_POST['password']);
                    Db::name('user')->strict(false)->insert($_POST);
                }else{
                    $this->error('该账户已经存在');
                }
            }else{
                $this->error('验证码输入错误');
            }

        }


    }
    //账号登录
    public function dologin()
    {
        $_POST['password'] = md5($_POST['password']);
        $num = Db::table('user')->where('user_name',$_POST['user_name'])->where('password',$_POST['password'])->find();
        if($num==NULL){
            return $this->error('该账号不存在');
        }else{
            // var_dump($num);die;
            Session::set('user_name',$num['user_name']);
            Session::set('user_id',$num['id']);
            session('session_start_time', time());//记录会话开始时间！判断会话时间的重点！重点！重点！

            // var_dump(Session::get('user_name'));die;
            return redirect('/home/index/index');
        }
    }
    //短信接口
    public function sendSms()
    {   
        $phone = $_POST['phoneNum'];
        // var_dump($phone);die;
        $code = rand(100000, 999999);

        $_SESSION['code'] = $code;

        // 配置信息
        $config = [
            'accessKeyId'    => 'LTAICz56hWdfWYgf',
            'accessKeySecret' => 'pKFewJgeW0sWDD8ejg1E1ZkxENx02r',
        ];
       // 使用方法二
        $client  = new Client($config);
        $sendSms = new SendSms;
        $sendSms->setPhoneNumbers($phone);
        $sendSms->setSignName('订餐系统注册验证码');
        $sendSms->setTemplateCode('SMS_130035028');
        $sendSms->setTemplateParam(['code' => $code ]);
        $sendSms->setOutId('demo');
        print_r($client->execute($sendSms));
    }

    //清除session
    public function logout()
    {
        Session::clear();
    }


    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
}
